﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;

namespace Txt2PajekLarge
{
    class PubHelper
    {
        //整数到字节数组的转换   
        public static byte[] intToByte(int number)
        {
            int temp = number;
            byte[] b = new byte[4];
            for (int i = b.Length - 1; i > -1; i--)
            {
                b[i] = Convert.ToByte(temp & 0xff);             //将最高位保存在最低位   
                temp = temp >> 8;               //向右移8位   
            }
            return b;
        }

        //字节数组到整数的转换   
        public static int byteToInt(byte[] b)
        {
            int s = 0;
            for (int i = 0; i < 3; i++)
            {
                if (b[i] >= 0)
                    s = s + b[i];
                else
                    s = s + 256 + b[i];
                s = s * 256;
            }
            if (b[3] >= 0)               //最后一个之所以不乘，是因为可能会溢出   
                s = s + b[3];
            else
                s = s + 256 + b[3];
            return s;
        }

        /// <summary>
        /// Test the DB
        /// </summary>
        /// <returns></returns>
        static public string TestDataBase()
        {
            string strResult;
            Database db = DatabaseFactory.CreateDatabase();
            try
            {
                if (GlobalPool.Settings != null && GlobalPool.Settings.BufferedIn==BufferIn.BUFFER_IN_DB)
                {
                    Assembly Asm = Assembly.GetExecutingAssembly();
                    Stream strm;

                    //more database type can be added here
                    switch (GlobalPool.Settings.DBType)
                    {
                        case "SQL Server":
                            strm = Asm.GetManifestResourceStream(Asm.GetName().Name + ".SQLServerScript.sql");
                            break;
                        default:
                            strm = Asm.GetManifestResourceStream(Asm.GetName().Name + ".SQLServerScript.sql");
                            break;
                    }
                    StreamReader reader = new StreamReader(strm);
                    string sql = "";
                    string strLine = reader.ReadLine();
                    while (strLine != null)
                    {
                        if (strLine.Trim() != "")
                        {
                            sql += " " + strLine;
                            if (strLine.LastIndexOf(';') == strLine.Length - 1)  //SQL语句结尾
                            {
                                sql = sql.Substring(0, sql.Length - 1);
                                db.CountByExecuteSQL(sql);
                                sql = "";
                            }
                        }
                        strLine = reader.ReadLine();
                    }

                    reader.Close();
                    reader.Dispose();

                    strResult = "OK";
                }
                else
                    strResult = "Application Settings Error!";
            }
            catch (Exception ex)
            {
                strResult = ex.Message;
            }
            return strResult;
        }

        //execute dos command
        public static void ExecCommand(string cmd, string inputs)
        {
            System.Diagnostics.Process aProcess = new System.Diagnostics.Process();

            aProcess.StartInfo.FileName = "cmd.exe";
            aProcess.StartInfo.UseShellExecute = false;
            aProcess.StartInfo.RedirectStandardInput = true;
            aProcess.StartInfo.RedirectStandardError = true;
            aProcess.StartInfo.CreateNoWindow = true;
            aProcess.StartInfo.ErrorDialog = false;
            if (inputs.Length > 0)
            {
                aProcess.StartInfo.RedirectStandardInput = true;
            }
            aProcess.Start();
            aProcess.StandardInput.WriteLine(cmd);
            if (inputs.Length > 0)
            {
                aProcess.StandardInput.WriteLine(inputs);
                aProcess.StandardInput.Close();
            }

            aProcess.WaitForExit(5000);
        }
    }
}
